package com.dlc.beijingrfid.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.dlc.commonlibrary.utils.RegexUtils;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.net.bean.LoginBean;
import com.dlc.beijingrfid.net.bean.SimpleBean;
import com.dlc.beijingrfid.net.exception.ErrorMsgException;
import com.dlc.beijingrfid.net.protocol.UserProto;
import com.dlc.beijingrfid.net.protocol.base.HttpProtocol;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/7/18 0018.
 */

public class ForgetPasswordActivity extends BaseActivity {
    @BindView(R.id.titleBar)
    TitleBar titleBar;
 /*   @BindView(R.id.getCheckCode)
    TextView getCheckCode;*/
    @BindView(R.id.phone_et)
    EditText phone_et;
    @BindView(R.id.sms_code_et)
    EditText sms_code_et;
    @BindView(R.id.new_psd_et)
    EditText new_psd_et;
    String phoneStr;
    private String errorText;
    String smsCodeStr;
    String newPsdStr;
    @Override
    protected int getLayoutID() {
        return R.layout.activity_forget_psd;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleBar.leftExit(this);
        int tab=getIntent().getExtras().getInt("tab");
        if(tab==0){
            titleBar.titleText.setText("忘记密码");
        }else {
            titleBar.titleText.setText("重置密码");
        }


    }


    public void getCheckCode(View view){
        phoneStr=phone_et.getText().toString();
        if(!checkPhone(phoneStr)){
            showToast(errorText);
            return;
        }
        showLoadingDialog();
        UserProto.get().getCheckCode(phoneStr, new HttpProtocol.Callback<SimpleBean>() {
            @Override
            public void onSuccess(SimpleBean simpleBean) {
                dismissLoadingDialog();
                showToast(simpleBean.msg);
            }
            @Override
            public void onFailed(int code, ErrorMsgException e) {
                dismissLoadingDialog();
                showToast(e.getMessage());

            }
        });
    }
    public void comfirm(View view){
        phoneStr=phone_et.getText().toString();
        smsCodeStr=sms_code_et.getText().toString();
        newPsdStr=new_psd_et.getText().toString();
        if(!checkPhone(phoneStr)){
            showToast(errorText);
            return;
        }else if(TextUtils.isEmpty(smsCodeStr)){
            showToast("短信验证码不能为空");
            return;
        }else if(TextUtils.isEmpty(newPsdStr)){
            showToast("密码不能为空");
            return;
        }
        showLoadingDialog();
        UserProto.get().resetPassword(phoneStr,newPsdStr,smsCodeStr, new HttpProtocol.Callback<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                dismissLoadingDialog();
                showToast(loginBean.msg);
                finish();

            }
            @Override
            public void onFailed(int code, ErrorMsgException e) {
                dismissLoadingDialog();
                showToast(e.getMessage());
            }
        });

    }

    private boolean checkPhone(String phone){
        if(TextUtils.isEmpty(phone)){
            errorText = "请输入您的手机号";
            return false;
        }else if (!RegexUtils.checkPhone(phone)){
            errorText="请输入正确的手机号";
            return  false;
        }else {
            return true;
        }

    }
}
